package GUI;

import java.util.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import Data.*;

public class DesktopPane extends JDesktopPane {
	static final int VERTICAL = 0;
	static final int HORIZONTAL = 1;
	static boolean shouldUpdate = false;
	public void paint(Graphics g) {
		Lines lines = Lines.getInstance();
		LinkedList<Line> ls = lines.getLines();
		super.paint(g);
		if (shouldUpdate) {
			super.updateUI();
		}
		//super.paint(g);
		//paintComponent(g);
		//super.updateUI();
			

		int size = ls.size();
		for (int i = 0; i < size; i++) {
			if(ls.get(i).getType() == Line.HAS_A) {
				drawHasA(g, ls.get(i));
			} else if(ls.get(i).getType() == Line.IS_A) {
				drawIsA(g, ls.get(i));
			}
		}

		shouldUpdate = !shouldUpdate;
		//System.out.println("sdfg");
	}
	
	public void drawHasA(Graphics g, Line l) {
		int xp, yp;
		int x1 = l.getHead().getX();
		int y1 = l.getHead().getY();
		int h1 = l.getHead().getHeight();
		int w1 = l.getHead().getWidth();
		int x2 = l.getTail().getX();
		int y2 = l.getTail().getY();
		int h2 = l.getTail().getHeight();
		int w2 = l.getTail().getWidth();
		int xm1, ym1, xm2, ym2;

		g.setColor(Color.RED);
		if (x1 + w1 < x2) {
			xp = x2 - (x2 - x1 - w1) / 2;
			ym1 = y1 + h1 / 2;
			ym2 = y2 + h2 / 2;
			yp = ym1 - ( ym1 - ym2) / 2;
			drawLine(g, HORIZONTAL, x1 + w1, ym1, xp, yp, x2, ym2);
		} else if (x2 + w2 <= x1) {
			xp = x1 - (x1 - x2 - w2) / 2;
			ym1 = y1 + h1 / 2;
			ym2 = y2 + h2 / 2;
			yp = ym1 - ( ym1 - ym2) / 2;
			drawLine(g, HORIZONTAL, x2 + w2, ym2, xp, yp, x1, ym1);
		} else if (y1 + h1 <= y2) {
			xm1 = x1 + w1 / 2;
			xm2 = x2 + w2 / 2;
			xp = xm1 - (xm1 - xm2) / 2;
			yp = y1 + h1 - (y1 + h1 - y2) / 2;
			drawLine(g, VERTICAL, xm1, y1 + h1, xp, yp, xm2, y2);
		} else if (y2 + h2 <= y1) {
			xm1 = x1 + w1 / 2;
			xm2 = x2 + w2 / 2;
			xp = xm1 - (xm1 - xm2) / 2;
			yp = y2 + h2 - (y2 + h2 - y1) / 2;
			drawLine(g, VERTICAL, xm1, y1, xp, yp, xm2, y2 + h2);
		} else {
			xm1 = x1 + w1 / 2;
			ym1 = y1 + h1 / 2;
			xm2 = x2 + w2 / 2;
			ym2 = y2 + h2 / 2;
			//g.drawLine(xm1, ym1, xm2, ym2);
		}
	}
	
	public void drawIsA(Graphics g, Line l) {
		int xp, yp;
		int x1 = l.getHead().getX();
		int y1 = l.getHead().getY();
		int h1 = l.getHead().getHeight();
		int w1 = l.getHead().getWidth();
		int x2 = l.getTail().getX();
		int y2 = l.getTail().getY();
		int h2 = l.getTail().getHeight();
		int w2 = l.getTail().getWidth();
		int xm1, ym1, xm2, ym2;

		g.setColor(Color.BLUE);
		if (y1 + h1 <= y2) {
			xm1 = x1 + w1 / 2;
			xm2 = x2 + w2 / 2;
			xp = xm1 - (xm1 - xm2) / 2;
			yp = y1 + h1 - (y1 + h1 - y2) / 2;
			drawLine(g, VERTICAL, xm1, y1 + h1, xp, yp, xm2, y2);
		} else if (y2 + h2 <= y1) {
			xm1 = x1 + w1 / 2;
			xm2 = x2 + w2 / 2;
			xp = xm1 - (xm1 - xm2) / 2;
			yp = y2 + h2 - (y2 + h2 - y1) / 2;
			drawLine(g, VERTICAL, xm1, y1, xp, yp, xm2, y2 + h2);
		} else if (x1 + w1 < x2) {
			xp = x2 - (x2 - x1 - w1) / 2;
			ym1 = y1 + h1 / 2;
			ym2 = y2 + h2 / 2;
			yp = ym1 - ( ym1 - ym2) / 2;
			drawLine(g, HORIZONTAL, x1 + w1, ym1, xp, yp, x2, ym2);
		} else if (x2 + w2 <= x1) {
			xp = x1 - (x1 - x2 - w2) / 2;
			ym1 = y1 + h1 / 2;
			ym2 = y2 + h2 / 2;
			yp = ym1 - ( ym1 - ym2) / 2;
			drawLine(g, HORIZONTAL, x2 + w2, ym2, xp, yp, x1, ym1);
		}
	}
	
	public void drawLine(Graphics g, int type, int x1, int y1, int x2, int y2, int x3, int y3) {
		if (type == HORIZONTAL) {
			g.drawLine(x1, y1, x2, y1);
			g.drawLine(x2, y1, x2, y3);
			g.drawLine(x2, y3, x3, y3);
		} else if (type == VERTICAL) {
			g.drawLine(x1, y1, x1, y2);
			g.drawLine(x1, y2, x3, y2);
			g.drawLine(x3, y2, x3, y3);
		}
	}
	
}
